package com.lwc.interceptor;

import org.springframework.util.Base64Utils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthIntercepor  implements HandlerInterceptor {

    /**
     * 拦截器
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //请求token
        String token=request.getHeader("x-token");
        //获取请求头 授权参数值
        String auth = request.getHeader("Authorization");
        if (null == auth) {
            //无权限 访问
            return false;
        }
        //传了参数 验证
        String[] tmp = auth.split("\\s+");
        //获取加密后的字符串
        String info = tmp[1];
        info = new String(Base64Utils.decode(info.getBytes()));
        tmp = info.split(":");
        if ("admin".equals(tmp[0]) && "1357".equals(tmp[1]))
            return true;//放行
        return false;
    }
}
